from flask import Flask, request, jsonify
from human.service import Service
from company.company import Company
from company.aivo import Aivo
import json
from tools.Logger import Logger
import traceback
import platform

current_os = platform.system()
print(current_os)
if current_os == "Linux":
    print('linux')
    #chromdb for linux
    __import__('pysqlite3')
    import sys
    sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")

#flask
#flask --app main run
app = Flask(__name__)
#aivo
aivo = Aivo()

# 全局错误处理器
@app.errorhandler(Exception)
def handle_error(error):
    error_message = str(error)
    #发生错误，获取当前info
    return Logger.error(error_message,aivo.info(),id(aivo))

@app.route("/info",methods=['POST'])
def info():
    return Logger.success(aivo.info())

#company
@app.route("/setupCompany",methods=['POST'])
def setupCompany():
    data = request.get_data(as_text=True)
    jsonObj = json.loads(data)
    company_id = str(jsonObj['company_id'])
    company_name = str(jsonObj['company_name'])
    config = jsonObj['config']
    #根据自动上线的公司上线
    c = Company(company_id,company_name,config=config)
    aivo.loadCompany(c)
    return Logger.success("setup company successed")


@app.route("/unloadCompany",methods=['POST'])
def unloadCompany():
    data = request.get_data(as_text=True)
    jsonObj = json.loads(data)
    company_id = str(jsonObj['company_id'])
    aivo.unloadCompany(company_id)
    return Logger.success("unload company successed")

#service
@app.route("/setupService",methods=['POST'])
def setupService():
    #每个公司需要上线的员工
    data = request.get_data(as_text=True)
    jsonObj = json.loads(data)

    company_id = str(jsonObj['company_id'])
    company = aivo.companyById(company_id)

    service_id = str(jsonObj['service_id'])
    service_name = str(jsonObj['service_name'])

    '''
    [
        {
            "q":"问题",
            "a":"答案"
        }
    ]
    '''
    template = ""
    if ('template' in jsonObj):
        template = jsonObj['template']
    if ('history' in jsonObj):
        history = jsonObj['history']
        a = Service(service_id,service_name)
        #打卡+历史消息
        company.serviceClockOn(a,history,template)
    else:
        a = Service(service_id,service_name)
        company.serviceClockOn(a)

    return Logger.success('setup service successed')

#service
@app.route("/unloadService",methods=['POST'])
def unloadService():
    data = request.get_data(as_text=True)
    jsonObj = json.loads(data)

    company_id = str(jsonObj['company_id'])
    company = aivo.companyById(company_id)

    service_id = str(jsonObj['service_id'])
    service = company.serviceById(service_id)
    print(company)
    company.serviceClockOff(service)

    return Logger.success('unload service successed')


@app.route("/chat",methods=['POST'])
def chat():
    #每个公司需要上线的员工
    data = request.get_data(as_text=True)
    jsonObj = json.loads(data)

    company_id = str(jsonObj['company_id'])
    company = aivo.companyById(company_id)

    service_id = str(jsonObj['service_id'])
    message = jsonObj['message']
    service = company.serviceById(service_id)
    result = service.chat(message)

    return Logger.success(result)

if __name__ == "__main__":
    app.run()
